package Offer;

import java.util.Stack;

public class Test22 
{

	public static void main(String[] args) 
	{
		int[] pushA={1,2,3,4,5};
		int[] popA={4,5,3,2,1};
		System.out.println(SelfStack.isPopOrder(pushA,popA));
	}
    public static boolean isPopOrder(int[] pushA,int [] popA)
	{
	   if (pushA==null||popA==null||pushA.length == 0 || popA.length == 0) 
	   {
	        return false;
	   }
	   Stack<Integer> stack = new Stack<Integer>();
	   int j = 0;
	   for (int i = 0; i < pushA.length; i++) 
	   {
	       stack.push(pushA[i]);
	       while (j < popA.length && stack.peek() == popA[j]) 
	       {
	           stack.pop();
	           j++;
	       }
	 
	   }
	   return stack.empty();
	}
}

class SelfStack{
	static boolean isPopOrder(int []pushA,int []popB){
		if(pushA == null || popB == null ||
				pushA.length <= 0 || popB.length <= 0){
			return false;
		}
		if(pushA.length != popB.length){
			return false;
		}
		java.util.Stack<Integer> tmpStack = new java.util.Stack<Integer>();
		int j = 0;
		for(int i = 0, length = pushA.length; i < length; i ++){
			tmpStack.push(pushA[i]);
			while (j < length && tmpStack.peek() == popB[j] ) {
				tmpStack.pop();
				j++;
			}
		}
		return tmpStack.isEmpty();
	}
}
